clearvars -except Globaloption option

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% This code reads in the VAR dynamics and produce the Campbell-Shiller decomposition in the paper
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Load data
breakpoint = 0;
n_coint_var = 3;

if exist('option', 'var') == 0
    option.splitsample = 2;
end

if option.splitsample == 0
    startdatenum = 1630;
    enddatenum = 1699;
elseif option.splitsample == 1
    startdatenum = 1700;
    enddatenum = 1794;
elseif option.splitsample == 2
    startdatenum = 1630;
    enddatenum = 1794;
end

loaddata_wstock_cy;

ttime = [startdate:enddate];

output_rp = 0.03 - cy;
filename = ['MAT/Netherland_cy_wstock_', num2str(round(output_rp * 10000)), '_', num2str(startdatenum), '_', num2str(enddatenum)];

%% Estimation
run cs_estimation.m

%% steady-state upper bound calculation
upper = exp(pxbar) * (mean(tau - g));

gdpreturn = output_rp + y0nom_10 - pi0;

filename1 = ['MAT/Netherland_cy_wstock_para_', num2str(round(output_rp * 10000)), '_', num2str(startdatenum), '_', num2str(enddatenum)];
save (filename1, 'k0x', 'k1x', 's', 'upper', 'pdX', 'pxbar', 'gdpreturn', 'debt', 'debt_book', 'pdG', 'pdT', 'Xraw', 'Psi')

%% bootstrap
run BS_compute.m

save (filename, 's', 'std_coeff', 'debt', 'debt_book','upper');

